home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_098 / thai / phrases.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  255 lines

  1.  
  2. #include <stdio.h>
  3. #include "quiz.h"
  4.  
  5. #define WORDS_FILE        "words.thai"
  6. #define SENTENCE_FILE    "sentences.thai"
  7.  
  8. static char words_file[ 30 ];
  9. static char sentence_file[ 30 ];
  10.  
  11.  
  12. extern FILE *fopen ();
  13. extern char *malloc ();
  14.  
  15.  
  16. int
  17. load_phrases ()
  18. {
  19.     if ( find ( WORDS_FILE , words_file )
  20.     &&   find ( SENTENCE_FILE , sentence_file )
  21.     &&   load ( words_file , &word_head , "word" )
  22.     &&   load ( sentence_file , &sentence_head , "sentence" ) )
  23.         return ( TRUE );
  24.     return ( FALSE );
  25. }
  26.  
  27.  
  28. static int
  29. load ( filename , head , desc )
  30. char *filename;
  31. struct thai_phrase *head;
  32. char *desc;
  33. {
  34.     FILE *fp;
  35.     int loaded;
  36.     int num_loaded;
  37.     int status;
  38.  
  39.     num_loaded = 0;
  40.     fp = fopen ( filename , "r" );
  41.     if ( fp == NULL ) {
  42.         printf ( "Failed to open '%s'\n" , filename );
  43.         return ( FALSE );
  44.     }
  45.     loaded = TRUE;
  46.     while ( ( status = read_phrase ( fp , &scan ) ) == 1 ) {
  47.         if ( !add_phrase ( head , &scan ) ) {
  48.             loaded = FALSE;
  49.             break;
  50.         }
  51.         num_loaded++;
  52.     }
  53.     if ( status < 0 ) {
  54.         loaded = FALSE;
  55.         printf ( "Read error in '%s', near line number %d\n" ,
  56.             filename , num_loaded * 4 );
  57.     }
  58.     fclose ( fp );
  59.     printf ( "Loaded %d %ss\n" , num_loaded , desc );
  60.     return ( loaded );
  61. }
  62.  
  63.  
  64. unload_phrases ()
  65. {
  66.     unload ( words_file , &word_head , "word" );
  67.     unload ( sentence_file , &sentence_head , "sentence" );
  68. }
  69.  
  70.  
  71. static
  72. unload ( filename , head , desc )
  73. char *filename;
  74. struct thai_phrase *head;
  75. char *desc;
  76. {
  77.     FILE *fp;
  78.     struct thai_phrase *p;
  79.     int num_saved;
  80.  
  81.     if ( file_changed ) {
  82.         fp = fopen ( filename , "w" );
  83.         if ( fp == NULL )
  84.             return;
  85.         num_saved = 0;
  86.         for ( p = head->next; p != NULL; p = p->next ) {
  87.             write_phrase ( fp , p );
  88.             num_saved++;
  89.         }
  90.         printf ( "Saved %d %ss\n" , num_saved , desc );
  91.         fclose ( fp );
  92.     }
  93.     /* not needed if C compiler auto-frees all malloc()s
  94.     while ( head->next != NULL )
  95.         del_phrase ( head , head->next );
  96.     */
  97. }
  98.  
  99.  
  100. int
  101. add_phrase ( head , phrase )
  102. struct thai_phrase *head , *phrase;
  103. {
  104.     struct thai_phrase *p;
  105.  
  106.     p = (struct thai_phrase *) malloc ( sizeof ( struct thai_phrase ) );
  107.     if ( p == NULL )
  108.         return ( FALSE );
  109.     p->thai = strsave ( phrase->thai );
  110.     p->phonetic = strsave ( phrase->phonetic );
  111.     p->english = strsave ( phrase->english );
  112.     if ( p->thai == NULL  ||  p->phonetic == NULL  ||  p->english == NULL )
  113.         return ( FALSE );
  114.     p->right = 0;
  115.     p->wrong = 0;
  116.     p->prev = head;
  117.     p->next = head->next;
  118.     if ( head->next != NULL )
  119.         head->next->prev = p;
  120.     head->next = p;
  121.     return ( TRUE );
  122. }
  123.  
  124.  
  125. del_phrase ( head , phrase )
  126. struct thai_phrase *head , *phrase;
  127. {
  128.     struct thai_phrase *p;
  129.  
  130.     for ( p = head->next; p != NULL; p = p->next ) {
  131.         if ( p == phrase ) {
  132.             if ( p->next != NULL )
  133.                 p->next->prev = p->prev;
  134.             if ( p->prev != NULL )
  135.                 p->prev->next = p->next;
  136.             free ( p->thai );
  137.             free ( p->phonetic );
  138.             free ( p->english );
  139.             free ( p );
  140.             return;
  141.         }
  142.     }
  143. }
  144.  
  145.  
  146. replace_phrase ( old , new )
  147. struct thai_phrase *old , *new;
  148. {
  149.     if ( new->thai != NULL  &&  new->thai[0] != '\0' ) {
  150.         if ( old->thai != NULL )
  151.             free ( old->thai );
  152.         old->thai = strsave ( new->thai );
  153.     }
  154.     if ( new->phonetic != NULL  &&  new->phonetic[0] != '\0' ) {
  155.         if ( old->phonetic != NULL )
  156.             free ( old->phonetic );
  157.         old->phonetic = strsave ( new->phonetic );
  158.     }
  159.     if ( new->english != NULL  &&  new->english[0] != '\0' ) {
  160.         if ( old->english != NULL )
  161.             free ( old->english );
  162.         old->english = strsave ( new->english );
  163.     }
  164. }
  165.  
  166.  
  167. /* 1 means OK, 0 means EOF, -1 means error */
  168. static int
  169. read_phrase ( fp , phrase )
  170. FILE *fp;
  171. struct thai_phrase *phrase;
  172. {
  173.     char buf[ MAX_STRING ];
  174.  
  175.     if ( fgets ( phrase->thai , MAX_STRING , fp ) == NULL )
  176.         return ( 0 );        /* EOF */
  177.     rmlast ( phrase->thai );
  178.     if ( fgets ( phrase->phonetic , MAX_STRING , fp ) == NULL )
  179.         return ( -1 );        /* unexpected EOF */
  180.     rmlast ( phrase->phonetic );
  181.     if ( fgets ( phrase->english , MAX_STRING , fp ) == NULL )
  182.         return ( -1 );        /* unexpected EOF */
  183.     rmlast ( phrase->english );
  184.     if ( fgets ( buf , MAX_STRING , fp ) == NULL )
  185.         return ( -1 );        /* unexpected EOF */
  186.     if ( sscanf ( buf , "%d %d" , &phrase->right , &phrase->wrong ) != 2 )
  187.         return ( -1 );        /* illegal syntax */
  188.     return ( 1 );            /* OK */
  189. }
  190.  
  191.  
  192. static
  193. rmlast ( str )
  194. char *str;
  195. {
  196.     int len;
  197.  
  198.     len = strlen ( str );
  199.     if ( len < 1 )
  200.         return;
  201.     if ( str[ len - 1 ] == '\n' )
  202.         str[ len - 1 ] = '\0';
  203. }
  204.  
  205.  
  206. static
  207. write_phrase ( fp , phrase )
  208. FILE *fp;
  209. struct thai_phrase *phrase;
  210. {
  211.     fprintf ( fp , "%s\n%s\n%s\n%d %d\n" ,
  212.         phrase->thai , phrase->phonetic , phrase->english ,
  213.         phrase->right , phrase->wrong );
  214. }
  215.  
  216.  
  217. static
  218. find ( filename , path )
  219. char *filename , *path;
  220. {
  221.     /* try the current directory */
  222.  
  223.     strcpy ( path , filename );
  224.     if ( access ( path , 4 ) >= 0 )
  225.         return ( TRUE );
  226.  
  227.     /* try a subdirectory called Thai */
  228.  
  229.     strcpy ( path , "thai/" );
  230.     strcat ( path , filename );
  231.     if ( access ( path , 4 ) >= 0 )
  232.         return ( TRUE );
  233.  
  234.     /* try the disk root directory */
  235.  
  236.     strcpy ( path , ":" );
  237.     strcat ( path , filename );
  238.     if ( access ( path , 4 ) >= 0 )
  239.         return ( TRUE );
  240.  
  241.     /* if mounted, try thai: */
  242.  
  243.     if ( Assigned ( "Thai" ) ) {
  244.         strcpy ( path , "Thai:" );
  245.         strcat ( path , filename );
  246.         if ( access ( path , 4 ) >= 0 )
  247.             return ( TRUE );
  248.     }
  249.  
  250.     /* ugg */
  251.  
  252.     printf ( stderr , "Failed to find '%s'\n" , filename );
  253.     return ( FALSE );
  254. }
  255.